<template>
    <div class="users-page">
        <h1 class="page-title">用户列表</h1>
        
        <!-- 搜索框 -->
        <div class="search-box">
            <input
                type="text"
                v-model="searchQuery"
                placeholder="搜索用户..."
                @input="handleSearch"
            />
        </div>

        <!-- 用户列表 -->
        <div class="user-list">
            <div v-for="user in filteredUsers" :key="user.id" class="user-card">
                <img :src="user.avatar" :alt="user.name" class="user-avatar" />
                <div class="user-info">
                    <h3>{{ user.name }}</h3>
                    <p>{{ user.email }}</p>
                    <div class="user-status" :class="user.status">
                        {{ user.status }}
                    </div>
                </div>
                <button @click="handleEdit(user)" class="edit-btn">
                    编辑
                </button>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            searchQuery: '',
            users: []
        }
    },

    computed: {
        filteredUsers() {
            return this.users.filter(user =>
                user.name.toLowerCase().includes(this.searchQuery.toLowerCase()) ||
                user.email.toLowerCase().includes(this.searchQuery.toLowerCase())
            )
        }
    },

    async mounted() {
        // 获取用户列表
        try {
            const response = await fetch('your-api-url/users')
            this.users = await response.json()
        } catch (error) {
            console.error('获取用户列表失败:', error)
        }
    },

    methods: {
        async handleSearch() {
            // 调用搜索 API
            try {
                const response = await fetch(`your-api-url/users/search?q=${this.searchQuery}`)
                this.users = await response.json()
            } catch (error) {
                console.error('搜索失败:', error)
            }
        },

        async handleEdit(user) {
            // 处理编辑用户
            try {
                const response = await fetch(`your-api-url/users/${user.id}`, {
                    method: 'PUT',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(user)
                })
                // 处理响应...
            } catch (error) {
                console.error('编辑用户失败:', error)
            }
        }
    }
}
</script>

<style scoped>
.users-page {
    padding: 24px;
    max-width: 1200px;
    margin: 0 auto;
    margin-top: 64px;
}

.page-title {
    font-size: 24px;
    margin-bottom: 24px;
}

.search-box {
    margin-bottom: 24px;
}

.search-box input {
    width: 100%;
    padding: 12px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 16px;
}

.user-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 24px;
}

.user-card {
    border: 1px solid #eee;
    border-radius: 8px;
    padding: 16px;
    display: flex;
    align-items: center;
    gap: 16px;
    background: white;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.user-avatar {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    object-fit: cover;
}

.user-info {
    flex: 1;
}

.user-info h3 {
    margin: 0 0 4px 0;
    font-size: 18px;
}

.user-info p {
    margin: 0;
    color: #666;
}

.user-status {
    display: inline-block;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    margin-top: 8px;
}

.user-status.active {
    background: #e6f7e6;
    color: #2e7d32;
}

.user-status.inactive {
    background: #ffebee;
    color: #c62828;
}

.edit-btn {
    padding: 8px 16px;
    background: #1976d2;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: background 0.3s;
}

.edit-btn:hover {
    background: #1565c0;
}
</style> 